package com.etime.controller;

import com.etime.pojo.Order;
import com.etime.pojo.User;
import com.etime.service.OrderService;
import com.etime.service.TicketService;
import com.etime.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private TicketService ticketService;
    @Autowired
    private UserService userService;
    @RequestMapping("getOrderByPage")
    public ModelAndView getOrderByPage(Integer page,ModelMap modelMap){
        PageInfo<Order> pageInfo=orderService.getOrderByPage(page);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("back/order");
        modelMap.addAttribute("data",pageInfo);
        return modelAndView;
    }
    @RequestMapping("getOrderByName")
    public ModelAndView getOrderByName(Integer page,String user_name,ModelMap modelMap){
        PageInfo<Order> pageInfo=orderService.getOrderByName(page,user_name);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("back/order");
        modelMap.addAttribute("data",pageInfo);
        modelMap.addAttribute("user_name",user_name);
        return modelAndView;
    }
    @RequestMapping("addOrder")
    public String addOrder(Order order){
        orderService.addOrder(order);
        //请求支付接口
        return "redirect:../pay?WIDout_trade_no="+order.getOrder_id()+"&WIDtotal_amount="+order.getPay_money()+"&WIDsubject="+order.getSeat_id();
    }

    @RequestMapping("paySuccess")
    public ModelAndView paySuccess(Integer order_id, ModelMap modelMap, HttpSession session){
        //修改支付成功的订单状态
        orderService.updateStatus(order_id);
        //通过order_id查询订单信息
        Order order = orderService.getOrderById(order_id);
        //修改余票量(列车编号，座位类型)
        ticketService.editTicketNum(order);

        //跳转到个人中页面，个人中心页面需要当前用户信息，当前用户购买的车票信息
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("forward/user");
        //获取到用户信息放入session中
        User user = userService.getUserById(order.getUser_id());
        session.setAttribute("resUser",user);
        //用户购买的车票信息
        List<Order> list = orderService.getOrderByUser(user.getUser_id());
        modelMap.addAttribute("orderList",list);
        return modelAndView;
    }

    @RequestMapping("getOrderByUser")
    public ModelAndView getOrderByUser(ModelMap modelMap, HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("forward/user");
        User user = (User)session.getAttribute("resUser");
        List<Order> list = orderService.getOrderByUser(user.getUser_id());
        modelMap.addAttribute("orderList",list);
        return modelAndView;
    }
}
